﻿//-----------------------------------------------------------------------
// <copyright file="OfficeMenuHelper.cs" company="LMT">
//     Copyright (c) by La Minh Thuận.
// </copyright>
// <author>thuanla1985@hotmail.com</author>
//-----------------------------------------------------------------------
namespace LMT.Forms
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Threading;

    /// <summary>
    /// The drop down color menu
    /// </summary>
    internal partial class OfficeMenuHelper : Form
    {
        /// <summary>
        /// 
        /// </summary>
        private Control parentControl;

        /// <summary>
        /// 
        /// </summary>
        private bool isFreeze = false;

        /// <summary>
        /// Gets the color plate.
        /// </summary>
        /// <value>The color plate.</value>
        public OfficeColorPalette ColorPalette
        {
            get { return this.office2007ColorPlate1; }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="OfficeMenuHelper"/> class.
        /// </summary>
        public OfficeMenuHelper()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Shows the specified parent.
        /// </summary>
        /// <param name="parent">The parent.</param>
        /// <param name="location">The location.</param>
        public void Show(Control parent, Point location)
        {
            parentControl = parent;
            this.Location = parent.PointToScreen(location);
            this.Show();
        }

        /// <summary>
        /// Conceals the control from the user.
        /// </summary>
        /// <PermissionSet>
        /// 	<IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/>
        /// 	<IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/>
        /// 	<IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence"/>
        /// 	<IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/>
        /// </PermissionSet>
        new public void Hide()
        {
            base.Hide();
            if (parentControl != null)
            {
                Form parentForm = parentControl.FindForm();
                if (parentForm != null)
                {
                    parentForm.BringToFront();
                }
            }
        }

        /// <summary>
        /// Handles the Deactivate event of the Office2007MenuHelper control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void Office2007MenuHelper_Deactivate(object sender, EventArgs e)
        {
            if (!isFreeze)
            {
                Hide();
            }
        }

        /// <summary>
        /// Handles the Leave event of the Office2007MenuHelper control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void Office2007MenuHelper_Leave(object sender, EventArgs e)
        {
            if (!isFreeze)
            {
                Hide();
            }
        }

        /// <summary>
        /// Handles the SelectedColorChanged event of the office2007ColorPlate1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void office2007ColorPlate1_SelectedColorChanged(object sender, EventArgs e)
        {
            if (!isFreeze)
            {
                Hide();
            }
        }

        /// <summary>
        /// Office2007s the color plate1_ color palette selected.
        /// </summary>
        /// <param name="color">The color.</param>
        /// <returns></returns>
        DialogResult office2007ColorPlate1_ColorPaletteSelected(ref Color color)
        {
            isFreeze = true;
            try
            {
                ColorDialog dlg = new ColorDialog();
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    color = dlg.Color;
                    isFreeze = false;
                    return DialogResult.OK;
                }
            }
            finally
            {
                isFreeze = false;
            }
            return DialogResult.Cancel;
        }
    }
}